import Dialog from '../../../dist/dialog/dialog';
import { HomeHttp } from '../../../api/home.js'
const homeModel = new HomeHttp()

Component({

  options: {
    styleIsolation: 'isolated'
  },
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object,
      observer: function (newVal) {
        switch (newVal.seckillStatus) {
          case 1: // 热抢中
            this.setData({
              countTime: newVal.endTimestamp,
              seckillStatus: 1
            })
            break;
          case 2: // 已结束
            this.setData({
              countTime: 0,
              seckillStatus: 2
            })
            break;
          case 3: // 未开抢
            this.setData({
              countTime: newVal.startTimestamp,
              seckillStatus: 3
            })
            break;
          default:
            break;
        }
      }
    }, // 商品信息
    status: Number // 秒杀状态
  },

  /**
   * 组件的初始数据
   */
  data: {
    seckillStatus: 0, // 秒杀状态
    countTime: 0, // 倒计时
    countTimeDate: {},
    isAlert: true // 是否提醒
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 倒计时组件 - change
    onCountDownChange(event) {
      if (this.data.seckillStatus !== 2) {
        this.setData({
          countTimeDate: event.detail
        })
      }
    },

    // 倒计时组件 - finish
    onCountDownFinish() {
      let status = this.data.seckillStatus
      let setDataObj = {}
      switch (status) {
        case 1: // 热抢中
          setDataObj.seckillStatus = 2
          setDataObj.countTime = 1
          break
        case 2: // 已结束
          setDataObj.seckillStatus = 2
          setDataObj.countTime = 1
          break
        case 3: // 未开始
          setDataObj.seckillStatus = 1
          setDataObj.countTime = this.properties.info.endTimestamp
          break
        default:
          break
      }
      this.setData({
        seckillStatus: setDataObj
      })
    },
    onNavToSeckillGoods() {
      const { goodsId } = this.properties.info
      wx.navigateTo({
        url: `/pages/goods/seckillGoodsDetails/seckillGoodsDetails?id=${goodsId}`
      })
    },
    onAlert() {
      const { goodsId } = this.properties.info
      Dialog.confirm({
        title: '提醒我',
        message: '活动开始前5分钟提醒我'
      }).then(() => {
        homeModel.setAlert({
          business_type: 1,
          business_id: goodsId
        }).then(res => {
          const { info } = this.properties
          info.isSetAlert = true
          this.setData({
            info: info
          })
        })
      }).catch(() => {
        // on cancel
      });
    },
    onCancelAlert() {
      const { goodsId } = this.properties.info
      Dialog.confirm({
        title: '取消提醒',
        message: '是否确定取消提醒？'
      }).then(() => {
        homeModel.setAlert({
          business_type: 1,
          business_id: goodsId
        }).then(res => {
          const { info } = this.properties
          info.isSetAlert = false
          this.setData({
            info: info
          })
        })
      }).catch(() => {
        // on cancel
      });
    }
  }
})
